package com.dd.ent.car.ui.camera;

import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dd.ent.car.R;
import com.dd.ent.car.common.bean.video.Video;
import com.dd.ent.car.common.config.UIConfig;
import com.dd.ent.car.service.user.UserService;
import com.dd.ent.car.service.video.VideoService;
import com.dd.ent.car.ui.BaseActivity;
import com.dd.ent.car.ui.empty.EmptyLayout;
import com.dd.ent.car.ui.widget.ProgressAlertDialog;
import com.pp.ent.base.log.MLog;
import com.pp.ent.core.manage.UIHandler;
import com.pp.ent.view.annotation.Inject;

import java.io.File;

import mabeijianxi.camera.MediaRecorderActivity;

import static com.dd.ent.car.common.config.UIConfig.UPLOAD_VIDEO_QI_NIU_PROGRESS;


/**
 * Created by jian on 2016/7/21 15:52
 * mabeijianxi@gmail.com
 */
public class SendSmallVideoActivity extends BaseActivity implements View.OnClickListener {
    private final static String TAG = SendSmallVideoActivity.class.getSimpleName();
    private String videoUri;
    private TextView tv_send;
    private TextView tv_cancel;
    private String videoScreenshot;
    private ImageView iv_video_screenshot;
    private EditText et_send_content;

    private AlertDialog dialog;

    @Inject
    private VideoService videoService;
    @Inject
    private UserService userService;
    private String mTitle;
    private String mPosterUrl;
    private ProgressAlertDialog mProgressDialog;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        initData();
        initEvent();
    }

    private void initEvent() {
        tv_cancel.setOnClickListener(this);
        tv_send.setOnClickListener(this);
        et_send_content.setOnClickListener(this);
        iv_video_screenshot.setOnClickListener(this);
    }


    private void initData() {
        Intent intent = getIntent();
        videoUri = intent.getStringExtra(MediaRecorderActivity.VIDEO_URI);
        videoScreenshot = intent.getStringExtra(MediaRecorderActivity.VIDEO_SCREENSHOT);
        //Bitmap bitmap = BitmapFactory.decodeFile(videoScreenshot);
        //iv_video_screenshot.setImageBitmap(bitmap);
        Glide
                .with(this)
                .load(Uri.fromFile(new File(videoScreenshot)))
                .asBitmap()
                .into(iv_video_screenshot);
    }

    private void initView() {
        setContentView(R.layout.smallvideo_text_edit_activity);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_send = (TextView) findViewById(R.id.tv_send);
        et_send_content = (EditText) findViewById(R.id.et_send_content);
        iv_video_screenshot = (ImageView) findViewById(R.id.iv_video_screenshot);


    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_cancel:
                hesitate();
                break;
            case R.id.tv_send:
                videoService.addVideoToQiuNiu(videoScreenshot, UIConfig.UPLOAD_VIDEO_POSTER_QI_NIU_SUCCESS, UIConfig.UPLOAD_VIDEO_POSTER_QI_NIU_PROGRESS);
                showProgressDialog();
                hideSoftInput();
                break;
            case R.id.iv_video_screenshot:
                startActivity(new Intent(this, VideoPlayerActivity.class).putExtra(
                        "path", videoUri));
                break;
        }
    }


    @Override
    public void onBackPressed() {
        hesitate();
    }

    private void hesitate() {
        if (dialog == null) {
            dialog = new AlertDialog.Builder(this)
                    .setTitle(R.string.hint)
                    .setMessage(R.string.record_camera_exit_dialog_message)
                    .setNegativeButton(
                            R.string.record_camera_cancel_dialog_yes,
                            new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog,
                                                    int which) {
                                    finish();

//                                    FileUtils.deleteDir(getIntent().getStringExtra(MediaRecorderActivity.OUTPUT_DIRECTORY));

                                }

                            })
                    .setPositiveButton(R.string.record_camera_cancel_dialog_no,
                            null).setCancelable(false).show();
        } else {
            dialog.show();
        }
    }

    protected void dismissProgressDialog() {
        if (null != mProgressDialog) {
            mProgressDialog.dismiss();
            mProgressDialog = null;
        }
    }

    protected void showProgressDialog() {
        dismissProgressDialog();
        mProgressDialog = new ProgressAlertDialog(this);
        mProgressDialog.setCancelable(false);
        mProgressDialog.show();
    }

    @UIHandler(UIConfig.UPLOAD_VIDEO_QI_NIU_SUCCESS)
    public void updateVideoQiNiuResponse(String playUrl) {
        MLog.debug(TAG, "updateVideoQiNiuResponse playUrl=" + playUrl);
        Video video = new Video();
        video.setPlayUrl(playUrl);
        video.setPosterUrl(mPosterUrl);
        video.setUser(userService.getUser());
        video.setTitle(et_send_content.getText().toString());
        videoService.commitVideoInfo(video, userService.getUid());
    }

    @UIHandler(UIConfig.UPLOAD_VIDEO_POSTER_QI_NIU_SUCCESS)
    public void updateVideoPosterQiNiuResponse(String posterUrl) {
        MLog.debug(TAG, "updateVideoPosterQiNiuResponse posterUrl=" + posterUrl);
        mPosterUrl = posterUrl;
        videoService.addVideoToQiuNiu(videoUri, UIConfig.UPLOAD_VIDEO_QI_NIU_SUCCESS, UPLOAD_VIDEO_QI_NIU_PROGRESS);
    }

    @UIHandler(UIConfig.ADD_VIDEO_INFO_TO_SERVER_FAIL)
    public void addVideoInfoServerfail() {
        dismissProgressDialog();
        Toast.makeText(this, "上传失败", Toast.LENGTH_LONG).show();
    }

    @UIHandler(UIConfig.ADD_VIDEO_INFO_TO_SERVER_SUCCESS)
    public void addVideoInfoServerResponse() {
        dismissProgressDialog();
        Toast.makeText(this, "上传成功", Toast.LENGTH_LONG).show();
        finish();
    }

    @UIHandler(UIConfig.UPLOAD_VIDEO_QI_NIU_PROGRESS)
    public void uploadVideoQiNiuProgress(String key, double percent) {
        if (mProgressDialog != null) {
            mProgressDialog.setProgress((int) (percent * 100));
        }
    }


    private void hideSoftInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
